package com.qxzn.module.mine.activity;

import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v7.widget.AppCompatEditText;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;

import com.alibaba.fastjson.JSONObject;
import com.qxzn.app.api.api.HttpRequest;
import com.qxzn.app.api.util.EventBusManager;
import com.qxzn.app.bean.BaseBean;
import com.qxzn.app.bean.EventBusBean;
import com.qxzn.app.page.BaseActivity;
import com.qxzn.app.page.widget.ToastUtil;
import com.qxzn.app.util.Constant;
import com.qxzn.app.util.SharePreferenceUtil;
import com.qxzn.module.mine.R;
import com.qxzn.module.mine.R2;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ModifyNameActivity extends BaseActivity implements View.OnTouchListener {
    public static String EXTRA_TYPE = "type";
    public static String TYPE_USER = "user";
    public static String TYPE_ROBOT = "robot";

    private String type;

    @BindView(R2.id.modify_name_content)
    AppCompatEditText editText;

    @Override
    protected int initLayout() {
        return R.layout.activity_modify_name;
    }

    @Override
    protected void initView() {
        type = getIntent().getStringExtra(EXTRA_TYPE);
        editText.setText(TYPE_USER.equals(type) ? SharePreferenceUtil.getNickname(this) : SharePreferenceUtil.getDeviceName(this));
        editText.setSelection(editText.length());
        editText.setOnTouchListener(this);
    }

    @OnClick({R2.id.tv_right})
    public void onViewClicked(View view) {
        int i = view.getId();
        if (i == R.id.tv_right) {
            String name = editText.getText().toString().trim();
            if (TextUtils.isEmpty(name)) {
                ToastUtil.showTextToast(this, "昵称不能为空");
            } else {
                showProgress();
                JSONObject params = new JSONObject();
                if (TYPE_USER.equals(type)) {
                    params.put("username", SharePreferenceUtil.getUserName(this));
                    params.put("niname", name);
                    HttpRequest.getService().updateUserInfoDetail(HttpRequest.getDefaultBody(params)).enqueue(new Callback<BaseBean>() {
                        @Override
                        public void onResponse(Call<BaseBean> call, Response<BaseBean> response) {
                            hideProgress();
                            BaseBean baseBean = response.body();
                            if (baseBean.getCode() == 200) {
                                SharePreferenceUtil.setNickname(ModifyNameActivity.this, name);
                                EventBusManager.post(new EventBusBean.Builder().id(Constant.EventBusId.UPDATE_USER_INFO).build());
                                finish();
                            } else {
                                ToastUtil.showTextToast(ModifyNameActivity.this, baseBean.getMsg());
                            }
                        }

                        @Override
                        public void onFailure(Call<BaseBean> call, Throwable t) {
                            hideProgress();
                            ToastUtil.showTextToast(ModifyNameActivity.this, getString(R.string.network_error));
                        }
                    });
                } else {
                    params.put("robot_id", SharePreferenceUtil.getDeviceId(this));
                    params.put("robot_name", name);
                    HttpRequest.getService().updateRobotDetails(HttpRequest.getDefaultBody(params)).enqueue(new Callback<BaseBean>() {
                        @Override
                        public void onResponse(Call<BaseBean> call, Response<BaseBean> response) {
                            hideProgress();
                            BaseBean baseBean = response.body();
                            if (baseBean.getCode() == 200) {
                                SharePreferenceUtil.setDeviceName(ModifyNameActivity.this, name);
                                EventBusManager.post(new EventBusBean.Builder().id(Constant.EventBusId.SWITCH_DEVICE_ID).build());
                                finish();
                            } else {
                                ToastUtil.showTextToast(ModifyNameActivity.this, baseBean.getMsg());
                            }
                        }

                        @Override
                        public void onFailure(Call<BaseBean> call, Throwable t) {
                            hideProgress();
                            ToastUtil.showTextToast(ModifyNameActivity.this, getString(R.string.network_error));
                        }
                    });
                }
            }
        }
    }

    @Override
    protected void iniTitleBar() {
        tv_title.setText(TYPE_USER.equals(type) ? "设置昵称" : "设备昵称");
        tv_right.setVisibility(View.VISIBLE);
        tv_right.setText("完成");
        tv_right.setTextColor(Color.parseColor("#2A8CFF"));
    }

    @Override
    protected void initData(Bundle savedInstanceState) {

    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        // et.getCompoundDrawables()得到一个长度为4的数组，分别表示左右上下四张图片
        Drawable drawable = editText.getCompoundDrawables()[2];
        //如果右边没有图片，不再处理
        if (drawable == null)
            return false;
        //如果不是按下事件，不再处理
        if (event.getAction() != MotionEvent.ACTION_UP)
            return false;
        if (event.getX() > editText.getWidth()
                - editText.getPaddingRight()
                - drawable.getIntrinsicWidth()) {
            editText.setText("");
        }
        return false;
    }
}
